﻿using WorkflowCore.Interface;
using WorkflowCore.Models;

namespace Service.CertWorkFlow.Steps
{
    public class SendCertOrder : StepBodyAsync
    {
        public override async Task<ExecutionResult> RunAsync(IStepExecutionContext context)
        {
            var data = context.Workflow.Data as WorkflowData;
            var res = await data.CertService.Order(data.Config.Email, data.Config.Domain);
            data.Action?.Invoke(res.Message);
            if (res.Result != null)
            {
                data.Dns01ChallengeValidationDetails = res.Result;
                data.Action?.Invoke($"CA机构要求增加名称为:{data.Dns01ChallengeValidationDetails.DnsRecordName},类型为{data.Dns01ChallengeValidationDetails.DnsRecordType} 的解析{data.Dns01ChallengeValidationDetails.DnsRecordValue}");
            }
            else
            {
                throw new Exception(res.Message);
            }
            return ExecutionResult.Next();
        }
    }
}
